package com.wmeimob.fastboot.jiubei.service;

import com.wmeimob.fastboot.util.StringUtils;
import com.wmeimob.fastboot.jiubei.entity.UserAddress;
import com.wmeimob.fastboot.jiubei.mapper.UserAddressMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @ClassName UserAddressServiceImpl
 * @Description 服务类实现
 * @Author wjt
 * @Date Tue Aug 18 16:45:30 CST 2020
 **/

@Service
@Transactional(rollbackFor = {Exception.class})
@Slf4j
public class UserAddressServiceImpl implements UserAddressService {

    @Resource
    private UserAddressMapper userAddressMapper;


    @Override
    public List<UserAddress> list(UserAddress userAddress) {
        Example example = new Example(UserAddress.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(userAddress.getId())) {
            criteria.andEqualTo("id", userAddress.getId());
        }
        if (!StringUtils.isEmpty(userAddress.getUserId())) {
            criteria.andEqualTo("userId", userAddress.getUserId());
        }
        if (!StringUtils.isEmpty(userAddress.getName())) {
            criteria.andLike("name", StringUtils.fullFuzzy(userAddress.getName()));
        }
        if (!StringUtils.isEmpty(userAddress.getMobile())) {
            criteria.andLike("mobile", StringUtils.fullFuzzy(userAddress.getMobile()));
        }
        if (!StringUtils.isEmpty(userAddress.getProvinceId())) {
            criteria.andEqualTo("provinceId", userAddress.getProvinceId());
        }
        if (!StringUtils.isEmpty(userAddress.getProvince())) {
            criteria.andLike("province", StringUtils.fullFuzzy(userAddress.getProvince()));
        }
        if (!StringUtils.isEmpty(userAddress.getCityId())) {
            criteria.andEqualTo("cityId", userAddress.getCityId());
        }
        if (!StringUtils.isEmpty(userAddress.getCity())) {
            criteria.andLike("city", StringUtils.fullFuzzy(userAddress.getCity()));
        }
        if (!StringUtils.isEmpty(userAddress.getDistrictId())) {
            criteria.andEqualTo("districtId", userAddress.getDistrictId());
        }
        if (!StringUtils.isEmpty(userAddress.getDistrict())) {
            criteria.andLike("district", StringUtils.fullFuzzy(userAddress.getDistrict()));
        }
        if (!StringUtils.isEmpty(userAddress.getAddress())) {
            criteria.andLike("address", StringUtils.fullFuzzy(userAddress.getAddress()));
        }
        if (!StringUtils.isEmpty(userAddress.getIsDefault())) {
            criteria.andEqualTo("default", userAddress.getIsDefault());
        }
        if (!StringUtils.isEmpty(userAddress.getGmtCreate())) {
            criteria.andEqualTo("gmtCreate", userAddress.getGmtCreate());
        }
        if (!StringUtils.isEmpty(userAddress.getGmtModified())) {
            criteria.andEqualTo("gmtModified", userAddress.getGmtModified());
        }
        example.orderBy("id").desc();
        List<UserAddress> userAddresss = userAddressMapper.selectByExample(example);
        return userAddresss;
    }

    @Override
    public UserAddress queryUserAddressById(Object id) {
        return userAddressMapper.selectByPrimaryKey(id);
    }


    @Override
    public void addUserAddress(UserAddress userAddress) {
        userAddress.setGmtCreate(new Date());
        userAddressMapper.insertSelective(userAddress);
    }

    @Override
    public void removeUserAddress(Object id) {
        userAddressMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void modifyUserAddress(UserAddress userAddress) {
        userAddress.setGmtModified(new Date());
        userAddressMapper.updateByPrimaryKeySelective(userAddress);
    }

}
